//
//  SXCollectionManager.m
//  ShiXun
//
//  Created by lanou on 15/11/2.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXCollectionManager.h"

@implementation SXCollectionManager

// 收藏
+ (void)collectionDataWithTableName:(NSString *)tableName title:(NSString *)title urlID:(NSString *)urlID isCollection:(BOOL)isCollection finish:(finishCollection)finish
{
    NSString *docsdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dbpath = [docsdir stringByAppendingPathComponent:@"collection.sqlite"];
    FMDatabase *db = [FMDatabase databaseWithPath:dbpath];
    
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"create table if not exists %@ (title text, urlID text primary key)", tableName];
        BOOL rs = [db executeUpdate:sql];
        if (!rs) {
            NSLog(@"error create table");
        }
        else
        {
            if (isCollection) {
                NSString *collection = [NSString stringWithFormat:@"insert into %@ (title, urlID) values (?, ?)", tableName];
                BOOL rs = [db executeUpdate:collection, title, urlID];
                if (rs) {
                    finish(YES);
                }
                else
                {
                    finish(NO);
                }
            }
            else
            {
                NSString *collection = [NSString stringWithFormat:@"delete from %@ where urlID = ?", tableName];
                BOOL rs = [db executeUpdate:collection, urlID];
                if (rs) {
                    finish(YES);
                }
                else
                {
                    finish(NO);
                }
            }
        }
    }
    [db close];
}

// 查询是否收藏
+ (BOOL)checkCollectionWithTableName:(NSString *)tableName urlID:(NSString *)urlID
{
    NSString *docsdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dbpath = [docsdir stringByAppendingPathComponent:@"collection.sqlite"];
    FMDatabase *db = [FMDatabase databaseWithPath:dbpath];
    
    if ([db open]) {
        
        NSString *sql = [NSString stringWithFormat:@"create table if not exists %@ (title text, urlID text primary key)", tableName];
        if ([tableName isEqualToString:@"video"]) {
            sql = [NSString stringWithFormat:@"create table if not exists %@ (dataName blob, urlID text primary key)", tableName];
        }
        BOOL rs = [db executeUpdate:sql];
        if (!rs) {
            NSLog(@"error create table");
        }
        else
        {
            NSString *sql = [NSString stringWithFormat:@"select * from %@", tableName];
            FMResultSet *rs = [db executeQuery:sql];
            while ([rs next]) {
                NSString *url = [rs stringForColumn:@"urlID"];
                if ([url isEqualToString:urlID]) {
                    return YES;
                }
            }
        }

    }
    [db close];
    return NO;
}

// 查询某个表的全部数据
+ (NSArray *)checkCollectionWithTableName:(NSString *)tableName
{
    NSString *docsdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dbpath = [docsdir stringByAppendingPathComponent:@"collection.sqlite"];
    FMDatabase *db = [FMDatabase databaseWithPath:dbpath];
    
    NSMutableArray *allArray = [NSMutableArray array];
    
    if ([db open]) {
        
        NSString *sql = [NSString stringWithFormat:@"create table if not exists %@ (title text, urlID text primary key)", tableName];
        if ([tableName isEqualToString:@"video"]) {
            sql = [NSString stringWithFormat:@"create table if not exists %@ (dataName blob, urlID text primary key)", tableName];
        }
        BOOL rs = [db executeUpdate:sql];
        if (!rs) {
            NSLog(@"error create table");
        }
        else
        {
            NSString *sql = [NSString stringWithFormat:@"select * from %@", tableName];
            FMResultSet *rs = [db executeQuery:sql];
            while ([rs next]) {
                NSMutableArray *arr = [NSMutableArray array];
                if ([tableName isEqualToString:@"video"]) {
                    NSString *urlID = [rs stringForColumn:@"urlID"];
                    [arr addObject:urlID];
                    NSData *dataName = [rs dataForColumn:@"dataName"];
                    [arr addObject:dataName];
                    [allArray addObject:arr];
                }
                else
                {
                    NSString *title = [rs stringForColumn:@"title"];
                    [arr addObject:title];
                    NSString *urlID = [rs stringForColumn:@"urlID"];
                    [arr addObject:urlID];
                    [allArray addObject:arr];
                }
            }
        }
    }
    [db close];
    return allArray;
}

+ (void)collectionDataWithTableName:(NSString *)tableName dataName:(NSData *)dataName urlID:(NSString *)urlID isCollection:(BOOL)isCollection finish:(finishCollection)finish
{
    NSString *docsdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dbpath = [docsdir stringByAppendingPathComponent:@"collection.sqlite"];
    FMDatabase *db = [FMDatabase databaseWithPath:dbpath];
    
    if ([db open]) {
        NSString *sql = [NSString stringWithFormat:@"create table if not exists %@ (dataName blob, urlID text primary key)", tableName];
        BOOL rs = [db executeUpdate:sql];
        if (!rs) {
            NSLog(@"error create table");
        }
        else
        {
            if (isCollection) {
                NSString *collection = [NSString stringWithFormat:@"insert into %@ (dataName, urlID) values (?, ?)", tableName];
                BOOL rs = [db executeUpdate:collection, dataName, urlID];
                if (rs) {
                    finish(YES);
                }
                else
                {
                    finish(NO);
                }
            }
            else
            {
                NSString *collection = [NSString stringWithFormat:@"delete from %@ where urlID = ?", tableName];
                BOOL rs = [db executeUpdate:collection, urlID];
                if (rs) {
                    finish(YES);
                }
                else
                {
                    finish(NO);
                }
            }
        }
    }
    [db close];
}

@end
